/*
 * 描述： <描述>
 * 修改人： rain
 * 修改时间： 2016年8月9日
 * 项目： rainhy-servicelogger
 */
package com.rainhy.servicelogger.builder.console;

import com.google.gson.Gson;
import com.rainhy.core.exception.RainhyUnsupportedOperationException;
import com.rainhy.servicelogger.builder.BaseServiceLoggerBuilder;
import com.rainhy.servicelogger.logger.ServiceLogDecorate;
import com.rainhy.servicelogger.logger.ServiceLogPersister;
import com.rainhy.servicelogger.logger.ServiceLogQuerier;

/**
 * 业务日志建造者<br>
 * 生成控制台的业务日志
 * 
 * @author rain
 * @version [版本号, 2016年8月9日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ConsoleServiceLoggerBuilder extends BaseServiceLoggerBuilder {
    
    private static final Gson gson = new Gson();
    
    @Override
    protected ServiceLogDecorate buildServiceLoggerDecorate(final Class<?> loggerType) {
        return new ServiceLogDecorate() {
            @Override
            public Object decorate(Object log) {
                return log;
            }
        };
    }
    
    @Override
    protected ServiceLogPersister buildServiceLoggerPersister(final Class<?> loggerType) {
        return new ServiceLogPersister() {
            @Override
            public void persist(Object log) {
                String json = gson.toJson(log);
                System.err.println(loggerType.getName() + " |- " + json);
            }
        };
    }
    
    @Override
    protected ServiceLogQuerier buildServiceLoggerQuerier(final Class<?> loggerType) {
        return new ServiceLogQuerier() {
            @Override
            public Object find(String key) {
                throw new RainhyUnsupportedOperationException("[控制台业务日志建造者]不支持查询类操作.");
            }
            
            @Override
            public boolean isSupportQuery() {
                return false;
            }
        };
    }
    
    @Override
    protected boolean isSupport(Class<?> loggerType) {
        return true;
    }
    
}
